<?php

require_once dirname(dirname(__FILE__)) . '/config/config.php';
require_once DOCUMENT_ROOT . DIR_SHARED_CONFIG . 'db_connection.php';
require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . 'lib_date.php';

class dao_news {

    //Methods
    public function __construct() {
        
    }

    public function get_all() {
        $db = new db_connection();
        $con = $db->open_connect();

        $query = "SELECT * FROM tblnews";
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $list = array();
        while ($row = mysqli_fetch_array($result)) {
            array_push($list, $row);
        }

        $db->close_connect($con);
        return $list;
    }

    public function add($news_id, $news_title, $news_description, $file_upload) {
        $db = new db_connection();
        $con = $db->open_connect();
        if ($news_id == 0)
            $query = "INSERT INTO tblnews(News_Title, News_Description, Addedon, file_upload) VALUES ('" . $news_title . "', '" . $news_description . "', '" . lib_date::get_now() . "', '" . $file_upload . "')";
        else
            $query = "UPDATE tblnews SET News_Title = '" . $news_title . "', News_Description='".$news_description."', file_upload='".$file_upload."' WHERE News_Id = " . $news_id;
        mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $db->close_connect($con);
        return true;
    }

    public function get_by_id($news_id) {
        $db = new db_connection();
        $con = $db->open_connect();

        $query = "SELECT * FROM tblnews WHERE News_Id = " . $news_id;
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $row = mysqli_fetch_array($result);

        $db->close_connect($con);
        return $row;
    }

    public function delete($news_id) {
        $db = new db_connection();
        $con = $db->open_connect();

        $query = "DELETE FROM tblnews WHERE News_Id = " . $news_id;
        mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $db->close_connect($con);
        return true;
    }

    public function get_all_by_conference_id($conference_id) {
        $db = new db_connection();
        $con = $db->open_connect();
        $query = "SELECT * FROM tblnews WHERE conference_id = " . $conference_id;
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $list = array();
        while ($row = mysqli_fetch_array($result)) {
            array_push($list, $row);
        }
        $db->close_connect($con);
        return $list;
    }
    
    public function get_lastest_ones($number) {
        $db = new db_connection();
        $con = $db->open_connect();

        $query = "SELECT * FROM tblnews ORDER BY Addedon DESC LIMIT 0, " . $number;
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $list = array();
        while ($row = mysqli_fetch_array($result)) {
            array_push($list, $row);
        }
        $db->close_connect($con);
        return $list;
    }
    
}
